<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/15 0015
 * Time: 9:30
 */

namespace Cohuna\Models;
use Phalcon\Di;

/**
 * 交易记录表
 * @package Cohuna\Models
 */
class CohunaOrder extends ModelBase
{
    public $order_id;
    public $order_no;
    public $merchant_id;
    public $pay_id;
    public $transaction_id;
    public $out_transaction_id;
    public $trade_channel;
    public $trade_state;
    public $order_subject;
    public $body;
    public $buyer_id;
    public $seller_id;
    public $total_amount;
    public $discount_amount;
    public $operator_id;
    public $store_id;
    public $terminal_id;
    public $attach;
    public $currency;
    public $notify_url;
    public $time_expire;
    public $time_start;
    public $time_confirm;
    public $time_end;
    public $update_time;


    public function getSource()
    {
        return Di::getDefault()->getShared('config')->database->default->prefix . 'order';
    }
    public function beforeUpdate()
    {
        $this->update_time = date('Y-m-d H:i:s');
    }
    public function beforeCreate()
    {
        $this->time_start = date('Y-m-d H:i:s');
    }

    /**
     * 解析扩展信息
     * @param CohunaOrder $order
     * @return mixed
     */
    public static function getCohunaOrderExtendInfo($order)
    {
        $config = Di::getDefault()->getShared('config');

        if(isset($config->currency_list[$order->currency])){
            $order->currency_text = $config->currency_list[$order->currency] . "({$order->currency})";
        }else{
            $order->currency_text = '未知';
        }
        $cohuna_pay = CohunaPay::findFirstByPayId($order->pay_id);

        $order->pay_account = $cohuna_pay->pay_account;
        if(isset($config->trade_channel_list[$order->trade_channel])){
            $order->trade_channel_text =$config->trade_channel_list[$order->trade_channel];
        }else{
            $order->trade_channel_text = '未知';
        }
        if(isset($config->trade_state_list[$order->trade_state])){
            $order->trade_state_text = $config->trade_state_list[$order->trade_state];
        }else{
            $order->trade_state_text = '未知';
        }
        return $order;
    }
}